package com.itsu.job.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jerry.Su
 * @Date 2021/12/31 10:45
 */
@ConfigurationProperties(prefix = "itsu.job")
@Getter
@Setter
public class JobProperties {
    private List<Job> jobs = new ArrayList<>();

    private boolean enable;

    private final JobProperties.Pool pool = new JobProperties.Pool();

    private final JobProperties.Shutdown shutdown = new JobProperties.Shutdown();

    /**
     * Prefix to use for the names of newly created threads.
     */
    private String threadNamePrefix = "itsu-job-";

    private final Admin admin = new Admin();

    private boolean autoSchedule;

    private String rpcAdminAddress;

    private final Rpc rpc = new Rpc();

    @Getter
    @Setter
    public static class Admin {
        private String username;
        private String password;
        private String publicKey;
        private String privateKey;
    }


    @Getter
    @Setter
    public static class Pool {

        /**
         * Maximum allowed number of threads.
         */
        private int size = Runtime.getRuntime().availableProcessors() * 2;

    }

    @Getter
    @Setter
    public static class Shutdown {

        /**
         * Whether the executor should wait for scheduled tasks to complete on shutdown.
         */
        private boolean awaitTermination;

        /**
         * Maximum time the executor should wait for remaining tasks to complete.
         */
        private Duration awaitTerminationPeriod;

    }

    @Getter
    @Setter
    public static class Rpc {

        private int port = 8849;
        private String accessToken;
        private int httpMaxContentLength = 5 * 1024 * 1024;
        private int coreSize = Runtime.getRuntime().availableProcessors();
        private int maxPoolSize = Runtime.getRuntime().availableProcessors() * 2;
        private long keepAliveTime = 60L;
        private int workQueueSize = 2000;
    }

}
